﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DataModel;
using BussinesLayer.Services;
using WebMatrix.WebData;
using Mvc4.Filters;
using Newtonsoft.Json;
using DataAccess.Datatypes;

namespace Mvc4.Controllers
{
    public class CriterionToView
    {
        public long id { get; set; }
        public string Value { get; set; }
        public int SiteAttributeId { get; set; }
        public string SiteAttributeName { get; set; }
    }

    public class AttribteToView
    {
        public int id { get; set; }
        public string Name { get; set; }
        public int SiteId { get; set; }
    }

    public class WishListToView
    {
        public long id { get; set; }
        public decimal Latitude { get; set; }
        public decimal Longitude { get; set; }
        public decimal Radius { get; set; }
        public int SiteId { get; set; }
        public int UserId { get; set; }
        public IEnumerable<CriterionToView> Criterions { get; set; }
        public IEnumerable<AttribteToView> NotAssignedAttributes { get; set; }
    }

    public class WishListsController : Controller
    {
        private SiteServices ss = new SiteServices();
        private UserServices us = new UserServices();

        [Authorize]
        [InitializeSimpleMembership]
        public ActionResult MyWishList(int siteId, string siteName)
        {
            ViewBag.siteName = siteName + " - My Wish List";
            ViewBag.Name = siteName;
            int usrId = WebSecurity.CurrentUserId;
            DataAccess.Datatypes.WishList ws = us.GetWishList(usrId, siteId);

            var wltv = new WishListToView();
            wltv.SiteId = siteId;
            wltv.UserId = usrId;
            List<CriterionToView> crits = new List<CriterionToView>();
            List<AttribteToView> attrs = new List<AttribteToView>();

            wltv.id = ws.id;
            if (wltv.id > 0)
            {
                wltv.Latitude = ws.Latitude;
                wltv.Longitude = ws.Longitude;
                wltv.Radius = ws.Radius;
            }
            else
            {
                wltv.Latitude = 0;
                wltv.Longitude = 0;
                wltv.Radius = 1;
            }

            List<Criterion> criterions;

            if (ws.Criterions != null)
            {
                criterions = ws.Criterions.ToList();
            }
            else
            {
                criterions = new List<Criterion>();
            }

            ICollection<SiteAttributeSet> siteAttributes = ss.GetSiteData(siteId).Attributes;
            foreach (SiteAttributeSet sttr in siteAttributes)
            {
                var criterion = criterions.Where(x => x.SiteAttributeId == sttr.id);
                if (criterion.Count() == 0)
                {
                    attrs.Add(new AttribteToView { id = sttr.id, Name = sttr.Name, SiteId = sttr.SiteId });
                }
                else
                {
                    crits.Add(new CriterionToView
                    {
                        id = criterion.First().id,
                        Value = criterion.First().Value,
                        SiteAttributeId = criterion.First().SiteAttributeId,
                        SiteAttributeName = sttr.Name
                    });
                }
            }
            wltv.Criterions = crits;
            wltv.NotAssignedAttributes = attrs;

            return View(wltv);
        }

        [Authorize]
        [InitializeSimpleMembership]
        public ActionResult Wishes(int siteId, string siteName)
        {
            ViewBag.siteName = siteName + " - My Wish List";
            ViewBag.Name = siteName;

            Wishes wishes = us.GetGoodsWishList(WebSecurity.CurrentUserId, siteId);

            if (wishes == null)
            {
                return RedirectToAction("MyWishList/Edit", siteName);
            }//@Html.ActionLink("Edit", "Edit", (string)ViewBag.Name, new { id=item.Id }, null)
            else
            {
                return View(wishes);
            }
        }

    }
}